﻿#include "MirGoodsCell.h"
#include "../MirGraphics/MLibrary.h"
#include "../MirScenes/GameScene.h"
#include "../../Shared/Data/ClientData.h"

using namespace Client::MirGraphics;
using namespace Client::MirScenes;
using namespace SlimDX;

namespace Client::MirControls
{

    MirGoodsCell::MirGoodsCell()
    {
        Size tempVar(205, 32);
        setSize(&tempVar);
        setBorderColour(Color::Lime);

        NameLabel = new MirLabel();
        NameLabel->setAutoSize(true);
        NameLabel->setParent(this);
        NameLabel->setNotControl(true);
        Point tempVar2(44, 0);
        NameLabel->setLocation(&tempVar2);

        CountLabel = new MirLabel();
        CountLabel->setAutoSize(true);
        CountLabel->setParent(this);
        CountLabel->setNotControl(true);
        CountLabel->setDrawControlTexture(true);
        Point tempVar3(23, 17);
        CountLabel->setLocation(&tempVar3);
        CountLabel->setForeColour(Color::Yellow);

        PriceLabel = new MirLabel();
        PriceLabel->setAutoSize(true);
        PriceLabel->setParent(this);
        PriceLabel->setNotControl(true);
        Point tempVar4(44, 14);
        PriceLabel->setLocation(&tempVar4);

        NewIcon = new MirImageControl();
        NewIcon->setIndex(550);
        NewIcon->setLibrary(Libraries::Prguse);
        NewIcon->setParent(this);
        Point tempVar5(190, 5);
        NewIcon->setLocation(&tempVar5);
        NewIcon->setNotControl(true);
        NewIcon->setVisible(false);

        BeforeDraw += [&] (o, e)
        {
            Update();
        };
        AfterDraw += [&] (o, e)
        {
            DrawItem();
        };
    }

    void MirGoodsCell::Update()
    {
        NewIcon->setVisible(false);

        if (Item == nullptr || Item->Info == nullptr)
        {
            return;
        }
        NameLabel->setText(Item->Info->getFriendlyName());
        CountLabel->setText((Item->Count <= 1) ? "" : std::to_string(Item->Count));

        NewIcon->setVisible(!Item->IsShopItem || MultipleAvailable);

        if (UsePearls)
        {
            PriceLabel->setText(StringHelper::formatSimple("Price: {0} pearl{1}", static_cast<unsigned int>(Item->Price() * GameScene::NPCRate), Item->Price() > 1 ? "s" : ""));
        }
        else if (Recipe)
        {
            ClientRecipeInfo *recipe = GameScene::RecipeInfoList.SingleOrDefault([&] (std::any x)
            {
                return x::Item->ItemIndex == Item->ItemIndex;
            });

            PriceLabel->setText(StringHelper::formatSimple("Price: {0} gold", static_cast<unsigned int>(recipe->Gold * GameScene::NPCRate)));
        }
        else
        {
            PriceLabel->setText(StringHelper::formatSimple("Price: {0} gold", static_cast<unsigned int>(Item->Price() * GameScene::NPCRate)));
        }
    }

    std::vector<Vector2*> MirGoodsCell::getBorderInfo() const
    {
        if (getSize() == getSize()->Empty)
        {
            return std::vector<Vector2*>();
        }

        if (BorderRectangle != getDisplayRectangle())
        {
            _borderInfo = std::vector<Vector2*>
            {
                new Vector2(getDisplayRectangle()->Left - 1, getDisplayRectangle()->Top - 1),
                new Vector2(getDisplayRectangle()->Right, getDisplayRectangle()->Top - 1),
                new Vector2(getDisplayRectangle()->Left - 1, getDisplayRectangle()->Top - 1),
                new Vector2(getDisplayRectangle()->Left - 1, getDisplayRectangle()->Bottom),
                new Vector2(getDisplayRectangle()->Left - 1, getDisplayRectangle()->Bottom),
                new Vector2(getDisplayRectangle()->Right, getDisplayRectangle()->Bottom),
                new Vector2(getDisplayRectangle()->Right, getDisplayRectangle()->Top - 1),
                new Vector2(getDisplayRectangle()->Right, getDisplayRectangle()->Bottom),
                new Vector2(getDisplayRectangle()->Left + 40, getDisplayRectangle()->Bottom),
                new Vector2(getDisplayRectangle()->Left + 40, getDisplayRectangle()->Top - 1)
            };

            BorderRectangle = getDisplayRectangle();
        }
        return _borderInfo;
    }

    void MirGoodsCell::OnMouseEnter()
    {
        MirControl::OnMouseEnter();

        GameScene::Scene->CreateItemLabel(Item, false, false, GameScene::HideAddedStoreStats);
    }

    void MirGoodsCell::OnMouseLeave()
    {
        MirControl::OnMouseLeave();
        GameScene::Scene->DisposeItemLabel();
        GameScene::HoverItem = nullptr;
    }

    void MirGoodsCell::DrawItem()
    {
        if (Item == nullptr || Item->Info == nullptr)
        {
            return;
        }

        Size *size = Libraries::Items->GetTrueSize(Item->getImage());
        Point *offSet = new Point((40 - size->Width) / 2, (32 - size->Height) / 2);
        Libraries::Items->Draw(Item->getImage(), offSet->X + getDisplayLocation()->X, offSet->Y + getDisplayLocation()->Y);

        CountLabel->Draw();

        delete offSet;
    }
}
